package com.lmqb.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 日期转换工具类
 * @author LiuQianQian
 * 2017年9月4日 下午3:30:50
 */
public class DataUtils {
	/**
	 * 将日期从时间戳转为日期格式'yyyy-MM-dd HH:mm:ss'
	 * @return
	 */
	public static String formDateToStr(String dateInt) {
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    Long time=new Long(dateInt);  
	    String d = format.format(time*1000L);  
		return d;
	}
	/**
	 * 将日期从时间戳转为日期格式'yyyy-MM-dd'
	 * @param dateInt
	 * @return
	 */
	public static String formDate2Str(String dateInt) {
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");  
		Long time=new Long(dateInt);  
		String d = format.format(time*1000L);  
		return d;
	}
	/**
	 * 获取当前时间,如"2017-09-04"
	 * @return
	 */
	public static String findDayToday() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = calendar.getTime();
		String format = sdf.format(date);
		return format;
	}
	/**
	 * 获取当前日期的后一天，如"2017-09-05"
	 * @return
	 */
	public static String findDayAfter(Integer time) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		calendar.add(Calendar.DAY_OF_YEAR, time);
		Date date = calendar.getTime();
		String format = sdf.format(date);
		return format;
	}
	
	/**
	 * String类型时间转换为时间戳，"2017-09-04 16:06:00"-->1504512360000
	 * @return
	 * @throws ParseException 
	 */
	public static Long dataStrToTimestamp(String time) {
	    SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    Date date;
		try {
			date = format.parse(time);
			return date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		return null;
	}
	/**
	 * 时间戳转换为时间字符串	1504272599000L--->"2017-09-01 21:29:59"
	 * @param timeLong Long类型转换为string
	 * @return
	 */
	public static String timestampToDataStr(Long timeLong) {
		 SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Long time=new Long(timeLong);
		String d = format.format(time);
		return d;
	}
	public static void main(String[] args) {
		/*System.out.println(dataStrToTimestamp("2017-09-04 16:06:00"));//1504512360000
		System.out.println(findDayAfter());
		System.out.println(findDayToday());
		System.out.println(timestampToDataStr(1504272599000L));//2017-09-01 21:29:59
		System.out.println(timestampToDataStr(1504492222000L));//2017-09-04 10:30:22
*/		
		Map<String, Object> map = new HashMap<>();
		System.out.println(DataUtils.findDayAfter(0));
		//获取当前时间
		String today = DataUtils.findDayAfter(0) + " 00:00:00";
		Long statrDate = (long) (DataUtils.dataStrToTimestamp(today)*0.001);
		//获取后一天
		String dayafter = DataUtils.findDayAfter(1) + " 00:00:00";
		Long endDate = (long) (DataUtils.dataStrToTimestamp(dayafter)*0.001);
		map.put("statrDate", statrDate);
		map.put("endDate", endDate);
		System.out.println(map);
	}
}